package com.qys.livesMall.finance.service.impl;

import com.qys.livesMall.cache.utils.CacheUtils;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.finance.dao.FinanceAccountDao;
import com.qys.livesMall.finance.enums.AccountTradeTypeEnum;
import com.qys.livesMall.finance.enums.FinanceMessageEnum;
import com.qys.livesMall.finance.mapper.FinanceAccountMapper;
import com.qys.livesMall.finance.model.dto.AccountHandle;
import com.qys.livesMall.finance.model.dto.AccountSearch;
import com.qys.livesMall.finance.model.entity.FinanceAccount;
import com.qys.livesMall.finance.service.FinanceAccountDetailService;
import com.qys.livesMall.finance.service.FinanceAccountService;
import com.qys.livesMall.framework.view.Paging;
import com.qys.livesMall.framework.view.QysPage;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

import static com.qys.livesMall.finance.common.FinanceLockPrefix.ACCOUNT_LOCK;

/**
 * @menu FinanceAccountServiceImpl
 * @date 2023/07/29 16:12
 */
@Service("financeAccountService")
public class FinanceAccountServiceImpl extends AccountBaseServer implements FinanceAccountService {

    @Autowired
    private FinanceAccountDao financeAccountDao;

    @Autowired
    private FinanceAccountDetailService financeAccountDetailService;

    @Autowired
    private FinanceAccountMapper financeAccountMapper;


    @Override
    public FinanceAccount get(Integer type){
        return financeAccountDao.getByType(type);
    }

    @Override
    public QysPage<FinanceAccount> list(AccountSearch search) {
        Paging<FinanceAccount> page=financeAccountMapper.search(new Paging<>(search));
        if (CollectionUtils.isEmpty(page.getRecords())){
            return QysPage.build(search);
        }
        return QysPage.to(page);
    }

    @Override
    public AccountHandle directIn(AccountHandle handle) {
        //此处可用synchronized锁 -127~127
        synchronized (handle.getAccountType()) {
            BigDecimal inMoney = handle.getAmountIncurred().abs();

            //获取账户
            FinanceAccount account = financeAccountDao.getByType(handle.getAccountType());
            if (account == null) {
                handle.setSuccess(false);
                handle.setMessage(FinanceMessageEnum.PLATFORM_BALANCE_NOT_EXIST);
            }
            account.setBalance(account.getBalance().add(inMoney));
            handle.setTradeTypeEnum(AccountTradeTypeEnum.IN);
            //创建变更记录
            handle.setAmountIncurred(inMoney);
            financeAccountDetailService.add(handle, account);
            financeAccountDao.updateById(account);

            handle.setSuccess(true);
            return handle;
        }
    }

    @Override
    public AccountHandle directOut(AccountHandle handle) {
        synchronized (handle.getAccountType()) {
            BigDecimal outMoney = handle.getAmountIncurred().abs();
            //获取账户
            FinanceAccount account = financeAccountDao.getByType(handle.getAccountType());
            if (account == null) {
                handle.setSuccess(false);
                handle.setMessage(FinanceMessageEnum.PLATFORM_BALANCE_NOT_EXIST);
            }
            account.setBalance(account.getBalance().subtract(outMoney));
            handle.setTradeTypeEnum(AccountTradeTypeEnum.OUT);

            //创建变更记录
            handle.setAmountIncurred(outMoney);
            financeAccountDetailService.add(handle, account);
            financeAccountDao.updateById(account);

            handle.setSuccess(true);
            return handle;
        }
    }

}

